package com.taptech.doufu.ui.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;

import com.taptech.doufu.bean.cp.CpBean;
import com.taptech.doufu.ui.adapter.CpOrderListAdapter;
import com.taptech.doufu.ui.activity.NewCpDesActivity;
import com.taptech.doufu.ui.view.viewflow.view.ViewFlowAdImageAdapter;
import com.taptech.doufu.ui.view.TTHomeViewPager;
import com.taptech.doufu.R;
import com.taptech.doufu.constant.Constant;
import com.taptech.doufu.listener.HttpResponseListener;
import com.taptech.doufu.services.personalcenter.AccountService;
import com.taptech.doufu.services.CpService;
import com.taptech.doufu.util.DiaobaoUtil;
import com.taptech.doufu.util.UIUtil;
import com.taptech.doufu.net.httputils.HttpResponseObject;
import com.taptech.doufu.ui.view.PullToRefreshListView;
import com.taptech.doufu.ui.view.WaitDialog;

import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

public class MyOrderCpFragment extends SweepFragment implements HttpResponseListener
{

    protected PullToRefreshListView mSweepListView;
    protected CpOrderListAdapter listViewAdapter;
    protected String last = "";
    protected ArrayList<CpBean> mainList = new ArrayList<CpBean>();
    protected WaitDialog dialog;
    int current_type;
    public static final String ARGUMENT = "argument";
    public final static int TYPE_MY_CP = 2;
    private View no_content_iv, chose_cp_hint;
    private ViewFlowAdImageAdapter viewFlowAdapter;
    private TTHomeViewPager viewPager;

    public static MyOrderCpFragment newInstance(int type)
    {
        Bundle args = new Bundle();
        args.putInt(ARGUMENT, type);
        MyOrderCpFragment fragment = new MyOrderCpFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        Bundle bundle = getArguments();
        if (bundle != null)
        {
            current_type = bundle.getInt(ARGUMENT, 1);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
    {
        View fragment1Layout = inflater.inflate(R.layout.fragment_rank_cp_layout, container, false);
        initView(fragment1Layout);
        loadData("");
        return fragment1Layout;
    }

    @Override
    public void onResume()
    {
        super.onResume("MyOrderCpFragment", this.getActivity());
    }

    protected void initView(View parent)
    {

        chose_cp_hint = (View) parent.findViewById(R.id.chose_cp_hint);
        no_content_iv = (View) parent.findViewById(R.id.personal_draft_tip_view);
        listViewAdapter = new CpOrderListAdapter(getActivity(), mainList);
        mSweepListView = (PullToRefreshListView) parent.findViewById(R.id.rank_cp_list);
        mSweepListView.setRefreshable(true);
        mSweepListView.setLoadmoreable(true);
        mSweepListView.setAdapter(listViewAdapter);
        mSweepListView.setOnLoadAndRefreshListener(new PullToRefreshListView.OnLoadAndRefreshListener()
        {
            @Override
            public void onRefresh()
            {
                loadData("");
            }

            @Override
            public void loadMore()
            {

            }
        });
        mSweepListView.setOnScrollToBottomListener(new PullToRefreshListView.OnScrollToBottomListener()
        {
            @Override
            public void OnScrollToBottom()
            {
                loadData(last);
            }
        });
        mSweepListView.setOnItemClickListener(new AdapterView.OnItemClickListener()
        {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l)
            {

                int position = Integer.parseInt(Long.toString(l));
                if (position >= 0)
                {
                    Intent intent = new Intent(getActivity(), NewCpDesActivity.class);
                    CpBean data = (CpBean) mainList.get(position);
                    intent.putExtra(Constant.TOPIC_ID, data.getId());
                    intent.putExtra(Constant.TYPE, NewCpDesActivity.TYPE_CP);
                    startActivityForResult(intent, Constant.TAG_ADD_FROM_UGC);
                   /* Intent intent = new Intent(getActivity(), TagDesActivity.class);
                    CpBean data = (CpBean) mainList.get(position);
                    intent.putExtra(Constant.TOPIC_ID, data.getId());
                    intent.putExtra(Constant.OBJECT_TYPE, data.getObject_type());
                    getActivity().startActivity(intent);*/
                }
            }
        });
    }

    @Override
    public void handleResponse(int handleType, HttpResponseObject response)
    {
        try
        {
            switch (handleType)
            {
                case CpService.HANDLE_LOAD_PERSON_CPS_LIST:
                    if (response.getStatus() == Constant.STATUS_SUCCESS)
                    {
                        JSONObject data = (JSONObject) response.getData();
                        List<CpBean> tempList = DiaobaoUtil.json2list(CpBean.class, data.getJSONArray("cps"));
                        mainList.clear();
                        mainList.addAll(tempList);
                        listViewAdapter.notifyDataSetChanged();
                        mSweepListView.setFull(true);
                        mSweepListView.onRefreshComplete();
                        mSweepListView.loadMoreComplete();
                        if (tempList != null && tempList.size() > 0)
                        {
                            chose_cp_hint.setVisibility(View.GONE);
                        } else
                        {
                            chose_cp_hint.setVisibility(View.VISIBLE);
                        }
                    } else
                    {
                        UIUtil.toastMessage(getActivity(), DiaobaoUtil.getErrorTips(response));
                    }
                    break;
            }
          /*  if (mainList.size() > 0)
            {
                no_content_iv.setVisibility(View.VISIBLE);
            } else
            {
                no_content_iv.setVisibility(View.GONE);
            }*/
        } catch (Exception e)
        {
            e.printStackTrace();
        }

    }

    protected synchronized void loadData(String load_last)
    {
        last = load_last;
        String user_id = AccountService.getInstance().getUserUid();
        if (user_id != null)
        {
            CpService.getInstance().loadPersonalCPs(user_id, this);
        }

    }

    public void setViewPager(TTHomeViewPager mViewPager)
    {
        this.viewPager = mViewPager;
    }
}